<?php
namespace App\Services;

use App\Models\AdminModel;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;
use  App\Models\RoleModel;
use  App\Services\ModuleService;
use  App\Models\RoleSettingModel;
use Illuminate\Support\Facades\DB;

class RoleService
{
    //修改角色
    static public function modifyRole($data)
    {
        //参数验证
        $validator = Validator::make($data, [
            'id' =>'required|integer',//角色名
            'role_name' =>'required',//角色名
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $checkRoleInfo = RoleModel::where([
                                        'delete_flag' => 0,
                                        'role_name' => $data['role_name']
                                    ])
                                    ->where('id', '!=', $data['id'])
                                    ->first();

        if(!empty($checkRoleInfo)) {
            return ['code' => 1, 'msg' => '角色已存在', 'data' => ''];
        }

        RoleModel::where('id', $data['id'])->update(['role_name' => $data['role_name']]);
        return ['code' => 0, 'msg'=> 'success', 'data' => ''];
    }

    //设置角色权限
    static public function roleSetting($data)
    {
        //参数验证
        $validator = Validator::make($data, [
                'id' =>'required|integer',//角色名
                'first_module' =>'required',//角色名
                'second_module' =>'required',//角色名
            ],
            [
                'first_module.required' => '参数`一级权限模块`缺失',
                'second_module.required' => '参数`二级权限模块`缺失',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $isUpdate = RoleSettingModel::where('role_id', $data['id'])
                        ->update([
                            'first_module' => $data['first_module'],
                            'second_module' => $data['second_module'],
                        ]);

        if($isUpdate != false) {
            $roleUser = AdminModel::select('id')->where([
                'admin_role' => $data['id'],
                'parent_id' => 0
            ])->get()->toArray();

            $data = [];
            foreach($roleUser as $value) {
                $data[] = $value['id'];
            }
            Redis::SETEX('admin_array', 86400*15, json_encode($data));
        }

        return ['code' => 0, 'msg'=> 'success', 'data' => ''];
    }

    //角色权限详细
    static public function detail($id)
    {
        //角色内权限信息
        $roleSettingInfo = RoleSettingModel::where('role_id', $id)->first();
        $firstModuleArr = array_filter(explode(',', $roleSettingInfo['first_module']), function($v) { return $v !== ''; });
        $secondModuleArr = array_filter(explode(',', $roleSettingInfo['second_module']), function($v) { return $v !== ''; });

        $moduleInfo = ModuleService::roleModuleList();

        foreach($moduleInfo as $k => $v) {
            $moduleInfo[$k]['module_img'] = empty($v['module_img'])?'':$v['module_img'];
            if(in_array($v['id'], $firstModuleArr)) {
                $moduleInfo[$k]['is_enable'] = 1;
            }
            else {
                $moduleInfo[$k]['is_enable'] = 0;
            }

            if(!empty($v['second_module'])) {
                foreach($v['second_module'] as $k1 => $v1) {
                    if(in_array($v1['id'], $secondModuleArr)) {
                        $moduleInfo[$k]['second_module'][$k1]['is_enable'] = 1;
                    }
                    else {
                        $moduleInfo[$k]['second_module'][$k1]['is_enable'] = 0;
                    }
                }
            }
            else {
                $moduleInfo[$k]['second_module'] = [];
            }
        }

        return $moduleInfo;
    }
}
